import operator

__all__ = ["binary_search", "inplace_map"]



def binary_search(array, value, less_than = operator.lt):
    left = 0
    right = len(array) - 1
    while left <= right:
        mid = (left + right) >> 1
        if array[mid] == value:
            return True
        elif less_than(array[mid], value):
            left = mid + 1
        else:
            right = mid - 1
    return False


def find_if(func, iterable):
    for x in iterable:
        if func(x):
            return x
    return None


def inplace_map(func, array, start = 0, stop = None, step = 1):
    if stop is None:
        stop = len(array)
    for i in range(start, stop, step):
        array[i] = func(array[i])
